// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//
#pragma once

#include <melon/string.h>
#include <melon/init/init.h>
#include <gtest/gtest.h>
#include <string>
#include <unordered_set>
#include <vector>

#include <pollux/common/file/file_systems.h>
#include <pollux/testing/common/faulty_file_system.h>
#include <pollux/connectors/hive/hive_connector.h>
#include <pollux/dwio/common/file_sink.h>
#include <pollux/dwio/common/tests/FaultyFileSink.h>
#include <pollux/dwio/dwrf/register_dwrf_reader.h>
#include <pollux/dwio/dwrf/register_dwrf_writer.h>
#include <pollux/testing/exec/fuzzer_util.h>
#include <pollux/testing/exec/writer_fuzzer.h>
#include <pollux/testing/expression/fuzzer_toolkit.h>
#include <pollux/parse/type_resolver.h>
#include <pollux/serializers/presto_serializer.h>
#include <pollux/vector/vector_fuzzer.h>

namespace kumo::pollux::exec::test {

/// WriterFuzzerRunner leverages WriterFuzzer and VectorFuzzer to
/// automatically generate and execute table writer tests.
/// It works in following steps:
///
///  1. Pick different table write properties. Eg: partitioned, bucketed,
///  sorted.
///  2. Generate corresponding table write query plan.
///  3. Generate a random set of input data (vector).
///  4. Execute the query plan.
///  5. Generate corresponding sql in reference DB and execute the sql.
///  6. Assert results from step 4 and 5 are the same.
///  7. Rinse and repeat.
///
/// The common usage pattern is as following:
///
///  $ ./pollux_writer_fuzzer_test --steps 10000
///
/// The important flags that control WriterFuzzer's behavior are:
///
///  --steps: how many iterations to run.
///  --duration_sec: alternatively, for how many seconds it should run (takes
///          precedence over --steps).
///  --seed: pass a deterministic seed to reproduce the behavior (each iteration
///          will print a seed as part of the logs).
///  --batch_size: size of input vector batches generated.
///
/// e.g:
///
///  $ ./pollux_writer_fuzzer_test \
///         --steps 10000 \
///         --seed 123

class WriterFuzzerRunner {
 public:
  static int run(
      size_t seed,
      std::unique_ptr<ReferenceQueryRunner> referenceQueryRunner) {
    filesystems::registerLocalFileSystem();
    tests::utils::registerFaultyFileSystem();
    connector::registerConnectorFactory(
        std::make_shared<connector::hive::HiveConnectorFactory>());
    auto hiveConnector =
        connector::getConnectorFactory(
            connector::hive::HiveConnectorFactory::kHiveConnectorName)
            ->newConnector(
                kHiveConnectorId,
                std::make_shared<config::ConfigBase>(
                    std::unordered_map<std::string, std::string>()));
    connector::registerConnector(hiveConnector);
    dwrf::registerDwrfReaderFactory();
    dwrf::registerDwrfWriterFactory();
    dwio::common::registerFileSinks();
    dwio::common::registerFaultyFileSinks();
    kumo::pollux::exec::test::writerFuzzer(
        seed, std::move(referenceQueryRunner));
    // Calling gtest here so that it can be recognized as tests in CI systems.
    return RUN_ALL_TESTS();
  }
};

} // namespace kumo::pollux::exec::test
